﻿using System;
using System.Data;
using System.Web;
using BLL;

namespace MapServer
{
    public class LatLon: System.Web.HttpApplication
    {
        public void SetLatLon()
        {
            B_LatLonCorrection bLlc = new B_LatLonCorrection();
            HttpContext.Current.Application["LatLonC"] = bLlc.SelectCorrection();
        }
        private DataTable GetLatLon()
        {
            if (HttpContext.Current.Application["LatLonC"] == null)
            {
                return null;
            }
            return (DataTable)(HttpContext.Current.Application["LatLonC"]);
        }
        public bool GetLatLon(out double dLat, out double dLon,double dLat1,double dLon1)
        {
            dLat = dLat1;
            dLon = dLon1;
            int k = -1;
            double dLatLonC = 1;
            double dLatLonCd = 0;

            DataTable dDt = new DataTable();
            dDt = GetLatLon();
          
            if (dDt != null)
            {
                DataRowCollection dDr = dDt.Rows  ;
                for (int i = 0; i < dDt.Rows.Count; i++)
                {
                    dLatLonCd = Math.Abs(dLat - Convert.ToDouble( dDr[i]["lat"]))*Math.Abs(dLon - Convert.ToDouble( dDr[i]["lon"]));
                    if (dLatLonCd < dLatLonC)
                    {
                        dLatLonC = dLatLonCd;
                        k = i;
                    }
                }
            }
            if (k != -1)
            {
                dLat = dLat - Convert.ToDouble( dDt.Rows[k]["latc"]);
                dLon = dLon - Convert.ToDouble( dDt.Rows[k]["lonc"]);
            }
            return false;
        }
    }
}